//
// Created by xili on 2024/9/12 9:46.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int smallestRangeII(vector<int> &nums, int k) {
        sort(nums.begin(), nums.end());
        int res = nums.back() - nums[0];
        int n = nums.size();
        for (int i = 0; i < n - 1; ++i) {
            int l = min(nums[0] + k, nums[i + 1] - k);
            int h = max(nums[n - 1] - k, nums[i] + k);
            res = min(res, h - l);
        }

        return res;
    }
};
